package com.itheima.ydd.controller;




import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    IYddAdvimgService iYddAdvimgService;
    @Autowired
    IYddAdvposService iYddAdvposService;

    /**
     * 分页查询
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData pageR(String advimgdesc, String advimgpos, int page, int limit){
        //分页构造器
        Page<YddAdvimg> yddAdvimgPage = new Page<>(page, limit);
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);

        iYddAdvimgService.page(yddAdvimgPage,queryWrapper);

        List<HashMap> hashMaps = yddAdvimgPage.getRecords().stream().map(item -> {
            LambdaQueryWrapper<YddAdvpos> posLqw = new LambdaQueryWrapper<>();
            posLqw.eq(YddAdvpos::getId, item.getAdvimgpos());
            YddAdvpos yddAdvpos = iYddAdvposService.getOne(posLqw);
            HashMap hashMap = JSON.parseObject(JSON.toJSONString(item), HashMap.class);
            hashMap.put("advposname", yddAdvpos.getAdvposname());
            return hashMap;
        }).collect(Collectors.toList());

        HashMap<Object,Object> map = new HashMap<>();
        map.put("count", yddAdvimgPage.getCountId());
        map.put("totalPages", yddAdvimgPage.getPages());
        map.put("pageSize", yddAdvimgPage.getSize());
        map.put("currentPage", yddAdvimgPage.getCurrent());
        map.put("data", hashMaps);

        return ResultData.ok("",map);
    }

    /**
     * 广告位添加
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddAdvimg yddAdvimg){
        iYddAdvimgService.save(yddAdvimg);

        return ResultData.ok("","广告位添加成功");

    }

    /**
     * 删除单个或者多个广告图信息
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(int id){
        iYddAdvimgService.removeById(id);

        return ResultData.ok("",null);
    }

    /**
     * 数据回显
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id){
        YddAdvimg yddAdvimg = iYddAdvimgService.getById(id);

        return ResultData.ok("",yddAdvimg);
    }

    /**
     * 修改广告图信息
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg){
        iYddAdvimgService.updateById(yddAdvimg);

        return ResultData.ok("",null);
    }

}
